<?php

namespace app\service\admin\projects\shop;

use app\model\Common;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class ClassifyService
{

    /**
     * 分类列表
     * User : Du
     * Date : 2022/12/1
     * Time : 19:22
     * @param array $request
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList(array $request): array
    {
        $page = $request['page'] ?? 1;
        $size = $request['size'] ?? 10;
        $so = $request['so'] ?? '';
        $filter = [];
        if ($so) {
            $filter[] = ['name', 'like', '%' . $so . '%'];
        }
        $field = ['id', 'name', 'pid', 'create_time'];
        return Common::getList(GoodsClassifyTable, $filter, $field, $page, $size);
    }

    /**
     * 添加商品分类
     * User : Du
     * Date : 2022/12/1
     * Time : 19:08
     * @param array $request
     * @return int|string
     */
    public static function add(array $request)
    {
        if (!isset($request['name']) || !$request['name']) {
            return false;
        }
        return Common::addData(GoodsClassifyTable, $request);
    }

    /**
     * 修改分类
     * User : Du
     * Date : 2022/12/1
     * Time : 21:53
     * @param array $request
     * @return false|int
     * @throws DbException
     */
    public static function update(array $request)
    {
        if (!isset($request['id']) || !$request['id'] || !$request['name']) {
            return false;
        }
        $filter['id'] = $request['id'];
        return Common::updateData(GoodsClassifyTable, $filter, ['name' => $request['name']]);
    }

    /**
     * 删除分类
     * User : Du
     * Date : 2022/12/1
     * Time : 20:22
     * @param array $request
     * @return false|int|string
     * @throws DbException
     */
    public static function delete(array $request)
    {
        if (!isset($request['data']) || !$request['data']) {
            return false;
        }
        $ids = [];
        foreach ($request['data'] as $v) {
            $ids[] = $v['id'];
        }
        $filter[] = ['id', 'in', $ids];
        return Common::deleteData(GoodsClassifyTable, $filter);
    }
}